package view;

import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingWorker;

import net.sf.json.JSONObject;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

public class LoginView extends BaseView {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private JTextField username_text;
	private JPasswordField password_text;
	private JTextField randCode_text;
	private JLabel randCode_label;
	private Image image;

	// test
	private String username = "zhuchao941";
	private String password = "1231231";

	public LoginView() throws KeyManagementException, NoSuchAlgorithmException {
		initComponents();
	}

	private void initComponents() {
		JPanel panel = new JPanel();
		JLabel username_label = new JLabel("用户名");
		username_label.setBounds(37, 23, 50, 15);
		JLabel password_label = new JLabel("密码");
		password_label.setBounds(47, 54, 50, 15);
		JButton randCode_button = new JButton("看不清楚");
		randCode_button.setBounds(171, 82, 103, 28);
		randCode_label = new JLabel();
		randCode_label.setBounds(37, 84, 78, 26);
		//getRandCode();
		//异步得到验证码
		new SyncLoader().execute();
		randCode_button.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				getRandCode();
			}
		});
		username_text = new JTextField(20);
		username_text.setBounds(99, 20, 126, 21);
		password_text = new JPasswordField(20);
		password_text.setBounds(99, 51, 126, 21);
		randCode_text = new JTextField(5);
		randCode_text.setBounds(125, 84, 36, 26);
		// 设置初始值
		username_text.setText(username);
		password_text.setText(password);
		JButton button = new JButton("登陆");
		button.setBounds(99, 123, 93, 28);
		button.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					checkLogin();
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		});
		getContentPane().add(panel);
		panel.setLayout(null);
		panel.add(username_label);
		panel.add(username_text);
		panel.add(password_label);
		panel.add(password_text);
		panel.add(randCode_label);
		panel.add(randCode_button);
		panel.add(randCode_text);
		panel.add(button);

		setSize(300, 200);
		setVisible(true);
		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		setLocationRelativeTo(null);
	}

	private class SyncLoader extends SwingWorker<Object, Object> {

		@Override
		protected Object doInBackground() throws Exception {
			getRandCode();
			return null;
		}

	}

	private void getRandCode() {
		image = controller
				.getRandCode("https://kyfw.12306.cn/otn/passcodeNew/getPassCodeNew.do?module=login&rand=sjrand");
		randCode_label.setIcon(new ImageIcon(image));
	}

	private void checkLogin() throws Exception {
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		String username = username_text.getText();
		String password = String.valueOf(password_text.getPassword());
		String randCode = randCode_text.getText();
		nvps.add(new BasicNameValuePair("loginUserDTO.user_name", username));
		nvps.add(new BasicNameValuePair("userDTO.password", password));
		nvps.add(new BasicNameValuePair("randCode", randCode));
		String info = controller.login(
				"https://kyfw.12306.cn/otn/login/loginAysnSuggest", nvps);
		JSONObject jsonObject = JSONObject.fromObject(info);
		String message = jsonObject.getString("messages");
		// TODO 验证码还得改进
		if (message.length() > 2) {
			getRandCode();
			JOptionPane.showMessageDialog(this, message);
		} else {
			message = "登陆成功";
			JOptionPane.showMessageDialog(this, message);
			this.dispose();
			QueryView queryView = new QueryView();
			queryView.init();
		}
	}

	public static void main(String args[]) throws KeyManagementException,
			NoSuchAlgorithmException {
		new LoginView();
	}
}
